<?php
	
	if(isset($_GET['spectacle']))
	{
		$nomspectacle = sql_protect(html_escape($_GET['spectacle']));
	}
	
	$result = SGBD::query(
		"select count(*) as nb
		from prix 
		where nomspectacle='$nomspectacle';");
	$data = SGBD::fetch($result);
	$nb_types_prix = $data['nb'];
	
	$template->setTemplateFile('show_info_gestion_prix_name','tpl/show_info_gestion_prix_name.tpl');	
	$template->parseTemplate('show_info_gestion_prix_name',"NOM_SPECTACLE",$nomspectacle);
	
	$result = SGBD::query(
			"SELECT * from creneau
			WHERE nomspectacle='$nomspectacle'
			ORDER BY debut;"
			);
	if(!$result) {
	throw new Exception("Erreur lors de la requête SQL " . $result);
	}
	
	$template->setTemplateFile('show_list_achat',NULL); 
	
	while($data = SGBD::fetch($result)){
		$template->setTemplateFile('show_infos_achat','tpl/show_infos_achat.tpl');	
		$creneau = $data['pkcreneau'];
		$template->parseTemplate('show_infos_achat',"IDCRENEAU",$creneau);
		$template->parseTemplate('show_infos_achat',"DEBUT",$data['debut']);
		$template->parseTemplate('show_infos_achat',"SALLE",$data['nomsalle']);
		$var = sql_protect(html_escape($data['nomsalle']));
		$result2 = SGBD::query(
			"SELECT adressesalle
			FROM salle
			WHERE nomsalle='$var';");
		$data2 = SGBD::fetch($result2);
		$template->parseTemplate('show_infos_achat',"ADRESSE",$data2['adressesalle']);	
		$var = $data['cpcommune'];
		$result2 = SGBD::query(
			"SELECT nomcommune
			FROM commune
			WHERE cpcommune='$var';");
		$data2 = SGBD::fetch($result2);
		$template->parseTemplate('show_infos_achat',"COMMUNE",$data2['nomcommune']);	
		$template->parseTemplate('show_infos_achat',"CPCOMMUNE",$data['cpcommune']);
		$result2 = SGBD::query(
		"select distinct s.nbplace -
			(select (sum(c.nombre)) from reservation r, comporte c, creneau q
			WHERE r.numeroreserv=c.numeroreserv AND q.pkcreneau='$creneau' AND q.pkcreneau=r.creneau AND q.nomspectacle='$nomspectacle')
		AS nb_place
		FROM creneau q, salle s
		WHERE q.nomspectacle='$nomspectacle' AND q.nomsalle=s.nomsalle;");
		$data2 = SGBD::fetch($result2);
		$nbplace = $data2['nb_place'];
		if ($nbplace == "") {
			$result2 = SGBD::query(
				"select s.nbplace AS nbplace
				FROM creneau q, salle s
				WHERE q.nomspectacle='$nomspectacle' AND q.pkcreneau='$creneau' AND q.nomsalle=s.nomsalle;");
			$data2 = SGBD::fetch($result2);
			$nbplace = $data2['nbplace'];
		}
		
		if($nbplace>=100) {
			$nbplace = "Bonne";
		} else if ( ($nbplace<100) && ($nbplace>=50) ) {
			$nbplace = "Moyenne";
		} else if ( ($nbplace<50) && ($nbplace>=10) ) {
			$nbplace = "Faible";
		}
		
		$template->parseTemplate('show_infos_achat',"NOMBRE_PLACE",$nbplace);
		$template->parseTemplate('show_infos_achat',"NOMBRE_TYPE",$nb_types_prix);
		$template->parseTemplate('show_infos_achat',"NOM_SPECTACLE",$nomspectacle);
		

		
		$template->mergeTemplates('show_infos_achat','show_list_achat');
	}
	$template->mergeTemplates('show_list_achat','show_info_gestion_prix_name');
	$template->setTemplateFile('center','tpl/center.tpl');
	$template->parseTemplate('center',"CENTER_CONTENT",$template->getTemplate('show_info_gestion_prix_name'));

?>